<?php



class BlogModule extends CWebModule {
    public function init() {
        // this method is called when the module is being created
        // you may place code here to customize the module or the application
        // import the module-level models and components
        $this->setImport(array(
            'Blog.models.*',
            'Blog.components.*',
        ));
    }

    public function beforeControllerAction($controller, $action) {
        if (parent::beforeControllerAction($controller, $action)) {
            // this method is called before any module controller action is performed
            // you may place customized code here
            return true;
        } else {
            return false;
        }
    }

    public static function criteriaPost($lang = 'vi', $alias, $tag, $date, $limit, $title, $id, $priority = false) {
        $criteriaPost = new CDbCriteria();
        $lang         = $lang == 'en' ? 2 : 1;
        $criteriaPost->compare('lang', $lang);
        $criteriaPost->compare('t.status', APPROVED);
        $userId = Helper::user()->id ? Helper::user()->id : (Yii::app()->controller->id == 'site' ? 1 : '');
        if ($userId) {
            if (!Helper::user()->checkAccess('Super') && !Helper::user()->checkAccess('Administrators')) {
                $criteriaPost->compare('owner_id', $userId);
            }
        }
        if (is_array($limit) && $limit) {
            $criteriaPost->limit  = $limit['end'];
            $criteriaPost->offset = $limit['begin'];
        } elseif (intval($limit)) {
            $criteriaPost->limit = $limit;
        }
        if ($alias) {
            $criteriaPost->compare('alias', $alias);
        }
        if ($date) {
            $criteriaPost->compare('create_date', $date);
        }
        $criteriaPost->order = 'id desc';
        if ($tag) {
            $criteriaPost->addSearchCondition('tags', $tag);
        }
        if ($title) {
            $criteriaPost->addSearchCondition('title', $title);
        }
        if ($id) {
            $criteriaPost->compare('id', '<' . $id);
        }
        if ($priority) {
            $criteriaPost->compare('priority', $priority);
        }

        return $criteriaPost;
    }
}

